<?php

namespace app\admin\controller\channel;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\channel\AppChannel;
use app\admin\model\channel\ChannelDaily;
use app\common\controller\Backend;

class Channel extends Backend
{
    protected $model = null;
    protected $searchFields = '';
    protected $admin_id = null;
    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->assign('admin_id', $this->admin_id);
        $this->model = new ChannelDaily();

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model->where($where)->order($sort, $order)->paginate($limit);
            foreach ($list->items() as $v){
                //未绑定新增
                $v->un_reg_total = $v->reg_total>0?$v->un_reg_total."(".(round($v->un_reg_total/$v->reg_total,2)*100)."%)":0;
                //市场注册充值转化
                $v->un_new_recharge_num_p = $v->reg_total > 0 ?(round($v->un_new_recharge_num/$v->reg_total,2)*100).'%':0;
                //拉新绑定用户
                $tmp_reg_total = (int)$v->reg_total - (int)$v->un_reg_total;
                $n_reg_total = $tmp_reg_total>0?$tmp_reg_total:0;//绑定的新注册用户
                $n_reg_total_p = $v->reg_total>0?(round($n_reg_total/$v->reg_total,2)*100).'%':0;
                $v->n_reg_total = $n_reg_total>0?$n_reg_total."(".$n_reg_total_p.")":0;
                //拉新绑定充值人数
                $n_new_recharge_num = (int)$v->new_recharge_num - (int)$v->un_new_recharge_num;
              //  $n_new_recharge_num = $tmp_new_recharge_num > 0?$tmp_new_recharge_num:0;
                $v->n_new_recharge_num = $n_new_recharge_num > 0?$n_new_recharge_num:0;
                //拉新充值率
                $v->recharge_p = $v->reg_total>0? (round($n_new_recharge_num/$v->reg_total,2)*100).'%':0;
                $v->order_p = $v->reg_male > 0 ? (round($v->recharge_num/$v->reg_male,2)*100).'%' : 0;//充值转化率
                $v->new_recharge_rmb = round($v->new_recharge_coin/10, 2);
                $v->recharge_rmb = round($v->recharge_coin/10, 2);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        //获取所有的渠道信息
        $channel = AppChannel::getAll();
        $this->assign('channel',$channel);
        return $this->view->fetch();
    }

}